<html>
<head>
</head>
<body>
    <a id="a1" href="http://www.zoux.xin1">标签1</a>
    <a id="a2" href="http://www.zoux.xin2">标签2</a>
    <img id="a3" src="https://img.douyucdn.cn/data/yuba/weibo/2018/04/04/201804041625193205587261213.200x0.png"/>
    <input type="button" onclick="testA();" value="testA"/>
    <input type="button" onclick="testB();" value="testB"/>
    <input type="button" onclick="testC();" value="testC"/>
    <input type="button" onclick="test1();" value="test1"/>
    <div>
        <a href="dvi1.com"/>div1</a>
        <a href="div2.com"/>div2</a>

    </div>
    
</body>
<script>
//******************
function testA(){
    document.getElementById('a1').href="http://testA.ok";
}
function testB(){
    document.getElementById('a2').href="http://testB.ok";
}
function testC(){
    document.getElementById('a3').src="http://www.baidu.com";
}
function test1(){
    alert(document.getElementById('a3').src);
}
//****************************
// 全部小写
filterMap = {
    "a":"href",
    "div":"data-src",
    "div":"data-src",
    "script":"src",
    "span":"data-src",
    "link":"href",
    "form":"action",
    "img":"src"
};
toLower = function (str) {
    if (!str){
        return ;
    }
    return str.toLowerCase();
};
begin = function (x) {
    let childs ;
    try{
        childs = x.childNodes;
    }catch(err){
        console.log("catch:",err);
        return;
    }
    if (childs.length == 0) {
        console.log("length zero!");
        return;
    } else {
        for (var i = 0, len = childs.length; i < len; i++) {
            if (childs[i].childNodes.length != 0) {
                begin(childs[i]);
            }
            console.log("Tag In:",childs[i].tagName,childs[i]);
            let filter = filterMap[toLower(childs[i].tagName)]; // 这个let真的是。。。
            console.log("Filter:",filter);
            if (filter){
                console.log("在过滤map中:",childs[i]);
                let o=childs[i];
                Object.defineProperty(o,filter,{
                    set:function(v){
                        console.log("Object defineProperty set:",v);
                        o.setAttribute(filter,v);
                    },
                    get:function(){
                        return o.getAttribute(filter);
                    }
                });
            }
        }
    }
};
// bind body event DOMNodeInserted DOMSubtreeModified
document.addEventListener('DOMNodeInserted', function (e) {
    // innerHTML read content
    console.log("插入的节点:", e.target, e.target.tagName);
    begin(e.target);
});
begin(document.body);
console.log("ok");
</script>
</html>
